package com.liukai.todo_android.ui.activity;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.liukai.todo_android.R;
import com.liukai.todo_android.common.base.BaseActivity;
import com.liukai.todo_android.common.base.BaseFragment;
import com.liukai.todo_android.ui.fragment.HistoryFragment;
import com.liukai.todo_android.ui.fragment.MineFragment;
import com.liukai.todo_android.ui.fragment.TodoFragment;
import com.liukai.todo_android.common.utils.LogUtils;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.mainFrameLayout)
    FrameLayout mFrameLayout;

    @BindView(R.id.bottomNavigationView)
    BottomNavigationView mBottomNavigationView;
    private TodoFragment todoFragment;
    private HistoryFragment historyFragment;
    private MineFragment mineFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData(Context context) {

    }

    @Override
    protected void initView() {
        todoFragment = new TodoFragment();
        historyFragment = new HistoryFragment();
        mineFragment = new MineFragment();
        switchFragment(todoFragment);
    }

    @Override
    protected void initListener() {
// 底部导航栏点击事件
        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {

            switch (item.getItemId()) {
                // 点击书架按钮，切换至书架页面
                case R.id.todo:
                    switchFragment(todoFragment);
                    LogUtils.e(getClass(),"待办");
                    break;

                // 点击分类按钮，切换至分类页面
                case R.id.history:
                    switchFragment(historyFragment);
                    LogUtils.e(getClass(),"历史");
                    break;

                // 点击我的按钮，切换至我的页面
                case R.id.mine:
                    switchFragment(mineFragment);
                    LogUtils.e(getClass(),"我的");
                    break;

                default:
                    Toast.makeText(MainActivity.this, "点击失败", Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        });
    }

    /**
     * 切换Fragment
     *
     * @param baseFragment
     */
    private void switchFragment(BaseFragment baseFragment) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.mainFrameLayout, baseFragment);
        fragmentTransaction.commit();

    }

}